package com.jxinternet.platform.services.repository.convert;

import com.jxinternet.platform.services.entity.cassandra.UserCassandra;
import com.jxinternet.platform.services.entity.domian.User;
import com.jxinternet.platform.services.entity.mysql.UserMysql;
import org.springframework.stereotype.Service;

/**
 * Created by gold-jx on 2017/8/22.
 */
@Service
public class UserConvert implements BaseConvert<User, UserMysql, UserCassandra> {

    @Override
    public UserMysql convertMysql(User user) {
        if (user == null) {
            return null;
        }

        return copyEntityToMysql(user);
    }

    @Override
    public UserCassandra convertCassandra(User user) {
        if (user == null) {
            return null;
        }
        return copyEntityToCassandra(user);
    }

    @Override
    public User convertMysqlDomainToModel(UserMysql userMysql) {
        if (userMysql == null) {
            return null;
        }
        return copyMysqlToEntity(userMysql);
    }

    @Override
    public User convertCassandraDomainToModel(UserCassandra userCassandra) {
        if (userCassandra == null) {
            return null;
        }
        return copyCassandraToEntity(userCassandra);
    }


    private static User copyMysqlToEntity(UserMysql userMysql) {
        User user = new User();
        user.setId(userMysql.getId());
        user.setUserName(userMysql.getUserName());
        user.setPassword(userMysql.getPassword());
        user.setSalt(userMysql.getSalt());
        user.setNickName(userMysql.getNickName());
        user.setState(userMysql.getState());
        user.setPlatformNo(userMysql.getPlatformNo());
        user.setCreateTime(userMysql.getCreateTime());
        user.setUpdateTime(userMysql.getUpdateTime());
        user.setVersion(userMysql.getVersion());
        return user;
    }

    private static User copyCassandraToEntity(UserCassandra userCassandra) {
        User user = new User();
        user.setId(userCassandra.getId());
        user.setUserName(userCassandra.getUserName());
        user.setPassword(userCassandra.getPassword());
        user.setSalt(userCassandra.getSalt());
        user.setNickName(userCassandra.getNickName());
        user.setState(userCassandra.getState());
        user.setPlatformNo(userCassandra.getPlatformNo());
        //user.setCreateTime(userCassandra.getCreateTime());
        //user.setUpdateTime(userCassandra.getUpdateTime());
        //user.setVersion(userCassandra.getVersion());
        return user;
    }

    private static UserMysql copyEntityToMysql(User user) {
        UserMysql userMysql = new UserMysql();
        userMysql.setId(user.getId());
        userMysql.setUserName(user.getUserName());
        userMysql.setPassword(user.getPassword());
        userMysql.setSalt(user.getSalt());
        userMysql.setNickName(user.getNickName());
        userMysql.setState(user.getState());
        userMysql.setPlatformNo(user.getPlatformNo());
        userMysql.setCreateTime(user.getCreateTime());
        userMysql.setUpdateTime(user.getUpdateTime());
        userMysql.setVersion(user.getVersion());
        return userMysql;
    }

    private static UserCassandra copyEntityToCassandra(User user) {
        UserCassandra userCassandra = new UserCassandra();
        userCassandra.setId(user.getId());
        userCassandra.setUserName(user.getUserName());
        userCassandra.setPassword(user.getPassword());
        userCassandra.setSalt(user.getSalt());
        userCassandra.setNickName(user.getNickName());
        userCassandra.setState(user.getState());
        userCassandra.setPlatformNo(user.getPlatformNo());
        //userCassandra.setCreateTime(user.getCreateTime());
        //userCassandra.setUpdateTime(user.getUpdateTime());
        //userCassandra.setVersion(user.getVersion());
        return userCassandra;
    }
}
